package com.zhuangfei.hos_timetableview.ability;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.model.MySubject;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.listener.OnDateBuildAapter;
import com.zhuangfei.timetable.utils.ScreenUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;

import java.util.List;

public class DateAbility extends Ability {

    TimetableView mTimetableView;
    Button moreButton;
    String[] popupMenus;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_date);
        // 添加表头的include
        StackLayout stInclude = (StackLayout) findComponentById(ResourceTable.Id_st_include);
        stInclude.addComponent(LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_header_simple, stInclude, false));
        moreButton = (Button) findComponentById(ResourceTable.Id_id_more);
        moreButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showPopmenu();
            }
        });
        initTimetableView();
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        List<MySubject> mySubjects = SubjectRepertory.loadDefaultSubjects();
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .showView();
    }

    /**
     * 显示弹出菜单
     */
    public void showPopmenu() {
        if (popupMenus == null) {
            popupMenus = new String[]{"隐藏日期栏", "显示日期栏", "自定义日期栏", "恢复默认日期栏"};
        }
        new XPopup.Builder(getContext())
                .hasShadowBg(false)
                .atView(moreButton)
                .asAttachList(popupMenus, null,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                switch (text) {
                                    case "隐藏日期栏":
                                        hideDateView();
                                        break;
                                    case "显示日期栏":
                                        showDateView();
                                        break;
                                    case "自定义日期栏":
                                        customDateView();
                                        break;
                                    case "恢复默认日期栏":
                                        cancelCustomDateView();
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }, 0, 0).show();
    }

    /**
     * 隐藏日期栏
     */
    protected void hideDateView() {
        mTimetableView.hideDateView();
    }

    /**
     * 显示日期栏
     */
    protected void showDateView() {
        mTimetableView.showDateView();
    }

    /**
     * 自定义日期栏
     * 该段代码有点长，但是很好懂，仔细看看会有收获的，嘻嘻
     */
    protected void customDateView() {
        mTimetableView.callback(
                new OnDateBuildAapter() {
                    @Override
                    public Component onBuildDayLayout(LayoutScatter mInflate, int pos, int width, int height) {
                        int newHeight = ScreenUtils.vp2px(DateAbility.this, 30);
                        Component component = mInflate.parse(ResourceTable.Layout_item_custom_dateview, null, false);
                        Text dayTextView = (Text) component.findComponentById(ResourceTable.Id_id_week_day);
                        dayTextView.setText(dateArray[pos]);
                        layouts[pos] = (DirectionalLayout) component.findComponentById(ResourceTable.Id_id_week_layout);
                        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(width, newHeight);
                        layouts[pos].setLayoutConfig(lp);
                        return component;
                    }

                    @Override
                    public Component onBuildMonthLayout(LayoutScatter mInflate, int width, int height) {
                        int newHeight = ScreenUtils.vp2px(DateAbility.this, 30);
                        Component first = mInflate.parse(ResourceTable.Layout_item_custom_dateview_first, null, false);
                        // 月份设置
                        textViews[0] = (Text) first.findComponentById(ResourceTable.Id_id_week_month);
                        layouts[0] = null;
                        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(width, newHeight);
                        int month = Integer.parseInt(weekDates.get(0));
                        first.setLayoutConfig(lp);
                        textViews[0].setText(month + "\n月");
                        return first;

                    }
                }).updateDateView();
    }

    /**
     * 恢复默认日期栏
     */
    protected void cancelCustomDateView() {
        mTimetableView.callback((ISchedule.OnDateBuildListener) null)
                .updateDateView();
    }
}
